import { redirect } from 'react-router-dom';
import { MyRouteObject, checkAuth } from './index';
import { lazy } from 'react';

// 懒加载用户端组件
const UserLayout = lazy(() => import('@/layouts/UserLayout'));
const Home = lazy(() => import('@/pages/client/home'));
const ProblemList = lazy(() => import('@/pages/client/problem/List'));
const ProblemSetList = lazy(() => import('@/pages/client/problem/ProblemSetList'));
const ProblemSetDetail = lazy(() => import('@/pages/client/problem/ProblemSetDetail'));
const ProblemSetEdit = lazy(() => import('@/pages/client/problem/ProblemSetEdit'));
const PracticePage = lazy(() => import('@/pages/client/practice'));
const PracticeProblemSetDetail = lazy(() => import('@/pages/client/practice/ProblemSetDetail'));
const ContestList = lazy(() => import('@/pages/client/contest/List'));
const ContestDetail = lazy(() => import('@/pages/client/contest/Detail'));
const ContestRank = lazy(() => import('@/pages/client/contest/Rank'));
const DiscussionList = lazy(() => import('@/pages/admin/discussion/List'));
const DiscussionDetail = lazy(() => import('@/pages/admin/discussion/Detail'));
const Learn = lazy(() => import('@/pages/admin/learn'));
const Course = lazy(() => import('@/pages/admin/learn/Course'));
const CourseDetail = lazy(() => import('@/pages/admin/learn/CourseDetail'));
const Chapter = lazy(() => import('@/pages/admin/learn/Chapter'));
const PaperList = lazy(() => import('@/pages/client/paper/List'));
const PaperExam = lazy(() => import('@/pages/client/paper/PaperExam'));
const ExamResult = lazy(() => import('@/pages/client/paper/ExamResult'));
const Profile = lazy(() => import('@/pages/account/Profile'));
const CompleteProfile = lazy(() => import('@/pages/account/CompleteProfile'));

// 用户端路由配置
export const userRoutes: MyRouteObject[] = [
  {
    path: '/',
    element: <UserLayout />,
    children: [
      {
        index: true,
        element: <Home />,
        breadcrumb: '首页',
      },
      {
        path: 'profile',
        element: <Profile />,
        breadcrumb: '个人中心',
        loader: () => checkAuth() ? null : redirect('/login'),
      },
      {
        path: 'profile/complete',
        element: <CompleteProfile />,
        breadcrumb: '完善个人资料',
        loader: () => checkAuth() ? null : redirect('/login'),
      },
      {
        path: 'practice',
        children: [
          {
            index: true,
            element: <PracticePage />,
            breadcrumb: '练习',
            loader: () => checkAuth() ? null : redirect('/login'),
          },
          {
            path: 'problem-set/:id',
            element: <PracticeProblemSetDetail />,
            breadcrumb: '题单详情',
            loader: () => checkAuth() ? null : redirect('/login'),
          },
        ],
      },
      {
        path: 'problem',
        children: [
          {
            index: true,
            loader: () => redirect('/problem/list'),
          },
          {
            path: 'list',
            element: <ProblemList />,
            breadcrumb: '题目列表',
          },
          {
            path: 'problem-sets',
            element: <ProblemSetList />,
            breadcrumb: '题单管理',
          },
          {
            path: 'problem-set/:id',
            element: <ProblemSetDetail />,
            breadcrumb: '题单详情',
          },
          {
            path: 'problem-set/create',
            element: <ProblemSetEdit />,
            breadcrumb: '创建题单',
          },
          {
            path: 'problem-set/edit/:id',
            element: <ProblemSetEdit />,
            breadcrumb: '编辑题单',
          },

        ],
      },
      {
        path: 'contest',
        children: [
          {
            index: true,
            loader: () => redirect('/contest/list'),
          },
          {
            path: 'list',
            element: <ContestList />,
            breadcrumb: '比赛列表',
          },
          {
            path: 'rank/:id',
            element: <ContestRank />,
            breadcrumb: '比赛排名',
          },
          {
            path: ':id',
            element: <ContestDetail />,
            breadcrumb: '比赛详情',
          },
        ],
      },
      {
        path: 'discussion',
        children: [
          {
            index: true,
            element: <DiscussionList />,
            breadcrumb: '讨论列表',
          },
          {
            path: ':id',
            element: <DiscussionDetail />,
            breadcrumb: '讨论详情',
          },
        ],
      },
      {
        path: 'learn',
        children: [
          {
            index: true,
            element: <Learn />,
            breadcrumb: '学习',
          },
          {
            path: 'course',
            children: [
              {
                index: true,
                element: <Course />,
                breadcrumb: '课程列表',
              },
              {
                path: ':id',
                element: <CourseDetail />,
                breadcrumb: '课程详情',
                children: [
                  {
                    path: 'chapter/:chapterId',
                    element: <Chapter />,
                    breadcrumb: '章节内容',
                  }
                ]
              },
            ],
          },
        ],
      }
    ],
  },
  {
    path: 'client/paper',
    element: <UserLayout />,
    breadcrumb: '试卷',
    children: [
      {
        index: true,
        loader: () => redirect('/client/paper/list'),
        breadcrumb: '试卷列表',
      },
      {
        path: 'list',
        element: <PaperList />,
        breadcrumb: '试卷列表',
      },
      {
        path: 'exam/:id',
        element: <PaperExam />,
        breadcrumb: '答题',
        loader: () => checkAuth() ? null : redirect('/login'),
      },
      {
        path: 'result/:id',
        element: <ExamResult />,
        breadcrumb: '成绩查看',
        loader: () => checkAuth() ? null : redirect('/login'),
      },
    ]
  },
]; 